<?php
/**
 * @package		简易CMS
 * @subpackage	插件管理模块
 * @version		$Id: admincp_plugins.php 29 2013-05-07 06:45:56Z hopingsoft@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

if (! defined ( 'IN_JYCMS' ) || ! defined ( 'IN_ADMINCP' )) {
	exit ( 'Access Denied' );
}

cpheader ();

if (! empty ( $_GET ['identifier'] ) && ! empty ( $_GET ['pmod'] )) {
	$operation = 'config';
}

if ($operation != 'config' && ! $admincp->isfounder) {
	cpmsg ( 'noaccess_isfounder', '', 'error' );
}

$pluginid = ! empty ( $_GET ['pluginid'] ) ? intval ( $_GET ['pluginid'] ) : 0;
$anchor = ! empty ( $_GET ['anchor'] ) ? $_GET ['anchor'] : '';
$isplugindeveloper = isset ( $_G ['config'] ['plugindeveloper'] ) && $_G ['config'] ['plugindeveloper'] > 0;
if (! empty ( $_GET ['dir'] ) && ! ispluginkey ( $_GET ['dir'] )) {
	unset ( $_GET ['dir'] );
}
shownav ( 'extended', 'menu_plugins' );
require_once libfile ( 'function/plugin' );
require_once libfile ( 'model/plugins' );
require_once libfile ( 'class/plugins' );

if (! $operation) {
	/**
	 * 加载plugin文件夹
	 */
	$dir = ROOT_PATH . '/source/plugin';
	require_once libfile ( 'class/files' );
	$fs = cmsFS::listFolders ( $dir );
	$pluginlist = array ();
	if ($fs) {
		foreach ( $fs as $k => $v ) {
			$path = ROOT_PATH . '/source/plugin/' . $v;
			$files = cmsFS::listFiles ( $path );
			if (in_array ( $v . '.xml', $files )) {
				$pluginlist [$v] = $files;
			}
		}
	}
	if (! $pluginlist) {
		cpmsg ( '对不起，您的系统中暂时没有可用的插件！', '', 'error' );
	}
	$model = new plugins ();
	$sql = "SELECT * FROM #__common_plugins";
	DB::query ( $sql );
	$rows = DB::fetch_all ( '', 'directory' );

	$getvalue = array (
			'pluginid',
			'available'
	);
	$plugins = array ();
	foreach ( $pluginlist as $k => $v ) {
		$arr = $model->get_plugin ( $k );
		$arr ['exists'] = 1;
		unset ( $arr ['vars'] );
		$plugins [$k] = $arr;
		if (isset ( $rows [$k] )) {
			foreach ( $getvalue as $vv ) {
				$plugins [$k] [$vv] = $rows [$k] [$vv];
			}
			unset ( $rows [$k] );
		} else {
			foreach ( $getvalue as $vv ) {
				$plugins [$k] [$vv] = 0;
			}
		}
	}
	if ($rows) {
		foreach ( $rows as $k => $v ) {
			$v ['exists'] = 0;
			$plugins [$k] = $v;
		}
		unset ( $rows );
	}
	showtableheader ( 'plugins_list' );
	showsubtitle ( array (
			'插件名称',
			'插件类型',
			'插件描述',
			'版版',
			'作者',
			'可用',
			'文件存在',
			'操作'
	) );

	foreach ( $plugins as $k => $v ) {
		if ($v ['available']) {
			if ($v ['exists']) {
				$v ['available'] = '<a href="admincp.php?action=plugins&operation=del&id=' . $v ['pluginid'] . '">卸载</a>';
			} else {
				$v ['available'] = '是';
			}
		} elseif ($v ['pluginid']) {
			$v ['available'] = '否';
		} else {
			$v ['available'] = '未安装';
		}
		$v ['option'] = '';
		if ($v ['pluginid']) {
			if ($v ['exists']) {
				$v ['option'] .= '<a href="admin.php?action=plugins&operation=edit&id=' . $v ['pluginid'] . '">参数配置</a>&nbsp;';
			}
			$v ['option'] .= '<a href="admin.php?action=plugins&operation=del&id=' . $v ['pluginid'] . '">卸载</a>';
		} else {
			$v ['option'] = '<a href="admin.php?action=plugins&operation=install&directory=' . $v ['directory'] . '">安装</a>';
		}

		showtablerow ( '', array (), array (
				$v ['name'],
				$v ['group'],
				$v ['description'],
				$v ['version'],
				$v ['author'],
				$v ['available'],
				! empty ( $v ['exists'] ) ? '是' : '否',
				$v ['option']
		) );
	}
	showtablefooter ();
} elseif ($operation == 'install') {
	$name = trim ( getgpc ( 'directory' ) );
	if (! $name) {
		cpmsg ( '对不起，您没有指定要安装的插件！', 'action=plugins', 'error' );
	}
	$model = new plugins ();
	$arr = $model->get_plugin ( $name );
	if (! $arr) {
		cpmsg ( '对不起，您指定的插件不存在！', 'action=plugins', 'error' );
	}
	$vars = $arr ['vars'];
	$model->bind ( $arr );
	$result = $model->save ();
	if (! $result) {
		if (! $model->_error)
			$model->_error = '插件安装时出现未知错误，请联络管理员进行处理！';
		cpmsg ( $model->_error, 'action=plugins', 'error' );
	}
	$pid = $model->pluginid;
	$arr = $model->getPublicProperties ();
	$rows = array ();
	foreach ( $arr as $k ) {
		$rows [$k] = $model->$k;
	}
	$pv = new pluginvars ();
	if ($vars) {
		foreach ( $vars as $k => $v ) {
			$pv->pluginvarid = 0;
			$pv->pluginid = $pid;
			$pv->title = $v ['title'];
			$pv->variable = $k;
			$pv->value = $v ['default'];
			$pv->save ();
		}
		$co = count ( $vars ) + 1;
		$sql = "UPDATE #__common_pluginvars SET displayorder=$co-displayorder WHERE pluginid=$pid";
		DB::query ( $sql );
	}
	loadcache ( 'plugins', true );
	cpmsg ( '插件《' . $model->name . '》已经安装成功，现在可以去进行该插件的参数设置！', 'action=plugins&a=edit&id=' . $pid, 'succeed' );
} elseif ($operation == 'del') {
	$id = intval ( getgpc ( 'id' ) );
	if (! $id) {
		cpmsg ( '对不起，您没有指定要编辑的条目！', 'action=plugins', 'error' );
	}
	$model = new plugins ();
	$row = $model->load ( $id );
	if (! $row) {
		cpmsg ( '对不起，您指定要编辑的条目不存在！', 'action=plugins', 'error' );
	}
	$sql = "DELETE FROM #__common_pluginvars WHERE pluginid='$id'";
	DB::Query ( $sql );
	$model->delete ();
	cpmsg ( '指定的插件已经从系统中卸载了！', 'action=plugins', 'succeed' );
} elseif ($operation == 'edit') {
	$id = intval ( getgpc ( 'id' ) );
	if (! $id) {
		cpmsg ( '对不起，您没有指定要编辑的条目！', 'action=plugins', 'error' );
	}

	$model = new plugins ();
	$row = $model->load ( $id );
	if (! $row) {
		cpmsg ( '对不起，您指定要编辑的条目不存在！', 'action=plugins', 'error' );
	}
	$arr = $model->get_plugin ( $model->directory );
	if (! $arr) {
		cpmsg ( '对不起，您指定的插件不存在！', 'action=plugins', 'error' );
	}
	$vars = $arr ['vars'];
	if (submitcheck ( 'editsubmit' )) {
		$arr ['available'] = intval ( getgpc ( 'available' ) );
		$model->bind ( $arr );
		$result = $model->save ();
		if (! $result) {
			if (! $model->_error)
				$model->_error = '插件安装时出现未知错误，请联络管理员进行处理！';
			cpmsg ( $model->_error, 'action=plugins', 'error' );
		}
		$pv = new pluginvars ();
		if ($vars) {
			foreach ( $vars as $k => $v ) {
				$sql = "SELECT * FROM #__common_pluginvars WHERE pluginid='$id' AND variable='$k'";
				$row = DB::fetch_first ( $sql );
				if (! $row) {
					$row = array ();
					$row ['pluginvarid'] = 0;
					$row ['pluginid'] = $id;
					$row ['title'] = $v ['title'];
					$row ['variable'] = $k;
					$row ['value'] = $k;
					$row ['displayorder'] = 0;
				}
				$pv->bind ( $row );
				$pv->pluginid = $id;
				$pv->title = $v ['title'];
				$pv->variable = $k;
				$pv->value = getgpc ( $k );
				$result = $pv->save ();
				if (! $result) {
					if (! $pv->_error)
						$pv->_error = '保存时出错！';
					cpmsg ( $pv->_error, 'action=plugins', 'error' );
				}
			}
			$co = count ( $vars ) + 1;
			$sql = "UPDATE #__common_pluginvars SET displayorder=$co-displayorder WHERE pluginid=$id";
			DB::query ( $sql );
		}
		include_once libfile ( 'function/cache' );
		updatecache ( 'plugins' );
		cpmsg ( '插件《' . $model->name . '》的参数已经设置完成！', 'action=plugins', 'succeed' );
	} else {
		showformheader ( "plugins&operation=edit&id=$id" );
		showtableheader ();
		showtitle ( $model->name . '参数设置' );

		$pv = array (
				'name' => '插件名称',
				'group' => '插件类型',
				'description' => '插件描述',
				'author' => '作者',
				'copyright' => '版权',
				'date' => '创建日期',
				'lastmodified' => '最后更新日期'
		);
		foreach ( $pv as $k => $v ) {
			if ($arr [$k]) {
				showtablerow ( '', array (), array (
						$v,
						$arr [$k]
				) );
			}
		}
		showsetting ( '是否开启', 'available', $model->available, 'radio', '', 0, 'note', '', '', true );

		$sql = "SELECT * FROM #__common_pluginvars WHERE pluginid='$id'";
		DB::query ( $sql );
		$rows = DB::fetch_all ( '', 'variable' );
		$html .= label ( array (
				'type' => 'title',
				'title' => '参数列表'
		) );
		foreach ( $vars as $k => $v ) {
			$title = $v ['title'];
			if (! isset ( $v ['inputmode'] ) || ! $v ['inputmode'])
				$v ['inputmode'] = 'text';
			$inputmode = $v ['inputmode'];
			$value = $rows [$k] ['value'];
			$varname = $k;
			if (in_array ( $inputmode, array (
					'select',
					'mcheckbox',
					'binmcheckbox'
			) )) {
				$v ['option'] = explode ( ',', $v ['option'] );
				$option = array ();
				if ($v ['option']) {
					foreach ( $v ['option'] as $v )
						$option [] = array (
								trim ( $v ),
								trim ( $v )
						);
				}
				if (in_array ( $inputmode, array (
						'mcheckbox',
						'binmcheckbox'
				) )) {
					$value = unserialize ( $value );
				}
				$varname = array (
						$k,
						$option
				);
			}
			showsetting ( $title, $varname, $value, $inputmode, '', 0, $v ['description'], '', '', true );
		}
		showsubmit ( 'editsubmit' );
		showtablefooter ();
		showformfooter ();
	}
}
